app.controller('ActivityListController', [
  '$scope', 'Activity', 'activityList', '$location', function($scope, Activity, activityList, $location) {
    $scope.activityList = activityList;
    $scope.activityListClass = 'active';
    return $scope.detailView = function(activity) {
      return $location.path('/activity/detail/' + activity.id);
    };
  }
]);

app.controller('ActivityDetailController', [
  '$scope', '$http', 'activity', '$timeout', '$alert', '$location', function($scope, $http, activity, $timeout, $alert, $location) {
    $scope.activityListClass = 'active';
    $scope.activity = activity;
    return $scope.submit = function(sign, callback) {
      sign.activity_id = activity.id;
      return $http.post('/main/activity/' + activity.id + '/sign', sign).success(function() {
        $alert({
          title: '报名成功,后面会有社工联系您',
          placement: 'top',
          type: 'info',
          show: true,
          duration: 3
        });
        return callback();
      });
    };
  }
]);

app.config([
  '$routeProvider', function($routeProvider) {
    $routeProvider.when('/activity/list', {
      templateUrl: '/main/tmpl/activity_list',
      controller: 'ActivityListController',
      resolve: {
        activityList: function(Activity) {
          return Activity.query();
        }
      }
    });
    $routeProvider.when('/activity/detail/:id', {
      templateUrl: '/main/tmpl/activity_detail',
      controller: 'ActivityDetailController',
      resolve: {
        activity: function(Activity, $route) {
          return Activity.get({
            id: $route.current.params.id
          });
        }
      }
    });
    return $routeProvider.when('/activity', {
      redirectTo: '/activity/list'
    });
  }
]);
